package com.leecode;

/**
 * @Author: huangzhigao
 * @Date: 2022/4/8 14:32
 * 使括号有效的最少添加
 */
public class leecode921 {
    public static int minAddToMakeValid(String s) {
        //需要的右括号
        int need = 0;
        //需要的左括号
        int res = 0;
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '(') {
                need++;
            }
            if (chars[i] == ')') {
                need--;
                if (need == -1) {
                    res++;
                    need = 0;
                }
            }
        }
        return need + res;
    }

    public static void main(String[] args) {
        System.out.println( minAddToMakeValid("())"));
    }
}
